var bsurl = require('../../../utils/baseUrl.js');
var netUtils = require('../../../utils/netUtils.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productData: [],
    catelist: ['手机数码', '电脑办公','日用品','食品','家电', '男装', '女装', '男鞋', '女鞋'],
    limit: 6,
    offset: 0,
    keyWords:"",
    hidden:false,
    isCate:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.editTabBar(app.globalData.tabBar1);
    this.loadMore();
  },
  refesh: function () {

  },
  loadMore: function () {
    var body = {
      offset: this.data.offset,
      limit: this.data.limit,
      goods:{}
      
    }
    this.requestGoods("buyer/queryGoods.do", body);
  },
  navigate: function (e) {
    console.log(e.currentTarget.dataset.goods)
    wx.setStorageSync('goods', e.currentTarget.dataset.goods);
  },
  inputext:function(e){
    this.setData({keyWords:e.detail.value});
  },
  inputTouched:function(){
    this.setData({ hidden: true });
  },
  search:function(){
    if(!this.data.keyWords)
      return;
    this.setData({
      productData:[],
      offset: 0
    });
    var that = this;
    var body = {
      offset: this.data.offset,
      limit: this.data.limit,
      goods: { goodsname: that.data.keyWords, goodsdescribe: that.data.keyWords }
    }
    this.requestGoods("buyer/queryGoods.do", body);

    this.setData({ 
      keyWords:"",
      hidden: false ,
      offset:0
    });
  },
  cancle:function(){
    this.setData({ hidden: false ,keyWords:""});
  },
  //请求物品
  requestGoods:function(url,body){
    var that = this;
    netUtils.sendRequest(url,body, (data) => {
      console.log(that.data.limit)
      if (data.body.length == 0) {
        wx.showModal({
          title: '提示',
          content: '已经到底了',
        })
      }
      var list = that.data.productData.concat(data.body);
      that.setData({
        productData: list,
        offset: list.length
      });
    })
  },
  //查询时点击类别
  cateTouched:function(e){
    var sort = this.data.catelist[e.currentTarget.dataset.index];
    console.log(sort)
    this.setData({
      productData: [],
      offset: 0
    });
    this.cancle();
    var body={
      offset:this.data.offset,
      limit:this.data.limit,
      goods: { goodssort: sort }
    }
    this.requestGoods("buyer/queryGoods.do",body);
  }
})